package com.taotao.order.spring.config;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
@PropertySource(value = { "classpath:db.properties", "classpath:resource.properties" }, ignoreResourceNotFound = true)
@ComponentScan(basePackages = "com.taotao")
@SpringBootApplication
public class TaotaoApplication extends SpringBootServletInitializer {
	@Value("${jdbc.url}")
	private String jdbcUrl;
	@Value("${jdbc.driver}")
	private String jdbcDriverClassName;
	@Value("${jdbc.username}")
	private String jdbcUsername;
	@Value("${jdbc.password}")
	private String jdbcPassword;

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return builder.sources(TaotaoApplication.class);
	}

	@Bean(destroyMethod = "close")
	public DataSource dataSource() {
		DruidDataSource druidDataSource = new DruidDataSource();
		druidDataSource.setUrl(jdbcUrl);
		druidDataSource.setDriverClassName(jdbcDriverClassName);
		druidDataSource.setUsername(jdbcUsername);
		druidDataSource.setPassword(jdbcPassword);
		druidDataSource.setMaxActive(10);
		druidDataSource.setMinIdle(5);
		return druidDataSource;
	}

	public static void main(String[] args) {
		SpringApplication.run(TaotaoApplication.class, args);
	}
}
